package com.example.apiproject.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.apiproject.service.OssService;
import com.example.apiproject.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RequestMapping("/oss")
@RestController
public class OssController {

    @Autowired
    private OssService ossService;

    //上传头像，返回图片的url给
    @PostMapping("/upload")
    public R uploadOssFile(@RequestParam("file") MultipartFile file) throws Exception{
        //获取上传文件 MultipartFile
        //返回图片在oss上的路径
        String url = ossService.uploadFileAvatar(file);
        JSONObject object=new JSONObject();
        object.put("success",1);
        object.put("result","文件上传成功");
        object.put("url",url);
        return R.ok("文件上传成功").put("url",object);
    }
    @PostMapping("/upmutiload")
    public R upMutiLoadOssFile(@RequestParam("files") MultipartFile[] file) throws Exception{
        //获取上传文件 MultipartFile
        //返回图片在oss上的路径
        List<String> url = ossService.uploadArrayFile(file);
        JSONObject object=new JSONObject();
        object.put("success",1);
        object.put("result","文件上传成功");
        object.put("url",url);
        return R.ok("文件上传成功").put("url",url);
    }

}
